package com.xunfei.socket

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.xunfei.socket.Service.SocketClientService
import com.xunfei.socket.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var socketService: SocketClientService? = null
    private var isBound = false

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName, service: IBinder) {
            val binder = service as SocketClientService.LocalBinder
            socketService = binder.service
            isBound = true

            // 设置消息监听器
            socketService?.setMessageListener { message ->
                runOnUiThread {
                    binding.messageLog.append("收到: $message\n")
                }
            }

            // 设置连接状态监听器
            socketService?.setConnectionListener { isConnected ->
                runOnUiThread {
                    updateConnectionStatus(isConnected)
                }
            }
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
            isBound = false
            socketService = null
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        // 连接按钮点击事件
        binding.connectButton.setOnClickListener {
            if (socketService?.isConnected() == true) {
                socketService?.disconnect()
            } else {
                val host = binding.hostEditText.text.toString()
                val port = binding.portEditText.text.toString().toIntOrNull() ?: 8888

                if (host.isNotEmpty()) {
                    socketService?.connect(host, port)
                } else {
                    Toast.makeText(this, "请输入主机地址", Toast.LENGTH_SHORT).show()
                }
            }
        }

        // 发送按钮点击事件
        binding.sendButton.setOnClickListener {
            val message = binding.messageEditText.text.toString()
            if (message.isNotEmpty() && socketService?.isConnected() == true) {
                socketService?.sendMessage(message)
                binding.messageLog.append("发送: $message\n")
                binding.messageEditText.text.clear()
            } else if (socketService?.isConnected() != true) {
                Toast.makeText(this, "未连接到服务器", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        // 绑定服务
        Intent(this, SocketClientService::class.java).also { intent ->
            bindService(intent, connection, BIND_AUTO_CREATE)
        }
    }

    override fun onStop() {
        super.onStop()
        // 解绑服务
        if (isBound) {
            unbindService(connection)
            isBound = false
        }
    }

    private fun updateConnectionStatus(isConnected: Boolean) {
        binding.connectButton.text = if (isConnected) "断开连接" else "连接"
        binding.sendButton.isEnabled = isConnected
        binding.hostEditText.isEnabled = !isConnected
        binding.portEditText.isEnabled = !isConnected

        val statusText = if (isConnected) "已连接" else "未连接"
        binding.statusTextView.text = "连接状态: $statusText"
    }
}